package com.westar.tv.utils;

import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;

import com.tbruyelle.rxpermissions2.Permission;
import com.tbruyelle.rxpermissions2.RxPermissions;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * Created by ZWP on 2019/10/11 10:50.
 * 描述：动态获取权限
 */
public class RxPermissionsUtil {

    /**
     * 申请权限 activity中
     *
     * @param context
     * @param prms
     * @param callBack
     * @return
     */
    public static Disposable requestPermissions(AppCompatActivity context, final String[] prms, final IPermissionsCallBack callBack) {
        return new RxPermissions(context)
                .requestEachCombined(prms)
                .subscribe(new Consumer<Permission>() {
                    @Override
                    public void accept(Permission permission) throws Exception {
                        // 用户已经同意该权限
                        if (permission.granted) {
                            callBack.permissionSuccess(permission.name);
                        }
                        // 用户拒绝了该权限，没有选中『不再询问』（Never ask again）,那么下次再次启动时。还会提示请求权限的对话框
                        else if (permission.shouldShowRequestPermissionRationale) {
                            if (callBack != null) {
                                callBack.permissionErro(permission.name);
                            }
                        }
                        // 用户拒绝了该权限，而且选中『不再询问』
                        else {
                            if (callBack != null) {
                                callBack.permissionErro(permission.name);
                            }
                        }
                    }
                });
    }

    /**
     * 申请权限  fragment中
     *
     * @param fragment
     * @param prms
     * @param callBack
     * @return
     */
    public static Disposable requestPermissions(Fragment fragment, final String[] prms, final IPermissionsCallBack callBack) {
        return new RxPermissions(fragment)
                .requestEachCombined(prms)
                .subscribe(new Consumer<Permission>() {
                    @Override
                    public void accept(Permission permission) throws Exception {
                        // 用户已经同意该权限
                        if (permission.granted) {
                            callBack.permissionSuccess(permission.name);
                        }
                        // 用户拒绝了该权限，没有选中『不再询问』（Never ask again）,那么下次再次启动时。还会提示请求权限的对话框
                        else if (permission.shouldShowRequestPermissionRationale) {
                            if (callBack != null) {
                                callBack.permissionErro(permission.name);
                            }
                        }
                        // 用户拒绝了该权限，而且选中『不再询问』
                        else {
                            if (callBack != null) {
                                callBack.permissionErro(permission.name);
                            }
                        }
                    }
                });
    }


    public interface IPermissionsCallBack {
        /**
         * 权限请求失败
         *
         * @param name
         */
        void permissionErro(String name);

        /**
         * 权限请求成功
         *
         * @param name
         */
        void permissionSuccess(String name);
    }
}
